from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship
from create import engine, Base, session

class Role(Base):
    __tablename__ = 'role'
    rid = Column(Integer, primary_key=True, autoincrement=True)
    role_name = Column(String(10))

    def __repr__(self):
        output = "(%s, %s)" % (self.rid, self.role_name)
        return output

class UserOtm(Base):
    __tablename__ = 'user_otm'
    nid = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(10), nullable=False)
    role = Column(Integer, ForeignKey('role.rid'))
    group = relationship("Role", backref='uuu') # Role为类名

    def __repr__(self):
        output = "(%s, %s, %d)" % (self.nid, self.name, self.role)
        return output

Base.metadata.create_all(engine)

if __name__ == '__main__':
    # session.add(Role(role_name='dba'))
    # session.add(Role(role_name='sa'))
    # session.add(Role(role_name='net'))

    # session.add_all([
    #     UserOtm(name='fuzj', role='1'),
    #     UserOtm(name='jie', role='2'),
    #     UserOtm(name='张三', role='2'),
    #     UserOtm(name='李四', role='1'),
    #     UserOtm(name='王五', role='3'),
    # ])

    session.commit()